package cn.etynit.web.controller;

import cn.etynit.service.IVisitorLogService;
import cn.etynit.domain.VisitorLog;
import cn.etynit.query.VisitorLogQuery;
import cn.etynit.result.JsonResult;
import cn.etynit.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/visitorLog")
public class VisitorLogController {

    @Autowired
    public IVisitorLogService visitorLogService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody VisitorLog visitorLog){
        if(visitorLog.getId()!=null){
            visitorLogService.updateById(visitorLog);
        }else{
            visitorLogService.insert(visitorLog);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        visitorLogService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(visitorLogService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(visitorLogService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody VisitorLogQuery query){
        Page<VisitorLog> page = new Page<VisitorLog>(query.getPage(),query.getRows());
        page = visitorLogService.selectPage(page);
        return JsonResult.success(new PageList<VisitorLog>(page.getTotal(),page.getRecords()));
    }

    /**
     * 根据访客姓名查询访客记录
     * @param visitorName
     * @param visitorPhone
     * @return
     */
    @RequestMapping(value = "/{visitorName}/{visitorPhone}",method = RequestMethod.GET)
    public JsonResult getVisitor(@PathVariable("visitorName")String visitorName,@PathVariable("visitorPhone")String visitorPhone){
        return JsonResult.success(visitorLogService.getVisitor(visitorName,visitorPhone));
    }

}
